﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _09_Tetris
{
    internal class Information
    {
        struct InfoData
        {
            public string str;
            public ConsoleColor color;
        }


        InfoData[] infoList;
        int index;//指针
        int maxCount;//最大存储量
        int offsetX, offsetY;
        ConsoleKeyInfo key;
        bool isOpen;
        long lastTime, currentTime;
        int delay;
        int row, col;
        Input input;

        public Information(int x, int y, int delay, Input input)
        {
            Console.CursorVisible = false;
            Console.SetWindowSize(100, 30);
            Console.SetBufferSize(100, 30);
            index = 0;
            maxCount = 10;
            infoList = new InfoData[maxCount];
            offsetX = x;
            offsetY = y;
            isOpen = false;
            this.delay = delay * 10000;
            row = col = 0;
            lastTime = DateTime.Now.Ticks;
            this.input = input;
        }

        public void AddInfo(string str, ConsoleColor color)
        {
            if (index > maxCount - 1)
            {
                index = maxCount - 1;
                FowardInfoArray();
            }
            InfoData info = new InfoData();
            info.str = str;
            info.color = color;
            infoList[index++] = info;
        }

        void FowardInfoArray()
        {
            for (int i = 0; i < maxCount - 1; i++)
            {
                infoList[i] = infoList[i + 1];
            }
        }

        void PrintFrame()
        {
            MyPrint("                    ", offsetX, row + 2);
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == 9)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == 11 && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == 11 && j == 9)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == 11)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == 9)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    else
                    {
                        temp = "";
                    }
                    MyPrint(temp, offsetX + j, offsetY + i);
                }
            }
        }

        void PrinText()
        {
            for (int i = 0; i < row - 2; i++)
            {
                MyPrint(infoList[i].str, offsetX + 3, offsetY + 1 + i, infoList[i].color);
            }
        }

        public void PrintInfo()
        {
            PrintFrame();
            PrinText();
        }

        void MyPrint(string str, int x, int y, ConsoleColor color = ConsoleColor.White)
        {
            Console.SetCursorPosition(x * 2, y);
            Console.ForegroundColor = color;
            Console.Write(str);
            Console.ResetColor();
        }

        public void Input()
        {
            if (input.IsKeyPressed(ConsoleKey.O))
            {
                isOpen = !isOpen;
            }
        }

        public void Update()
        {
            currentTime = DateTime.Now.Ticks;
            if (currentTime - lastTime > delay)
            {
                if (isOpen)
                {
                    row++;
                    if (row > 12) row = 12;
                }
                else
                {
                    row--;
                    if (row < 0) row = 0;
                }
                lastTime = DateTime.Now.Ticks;
            }

        }
    }
}
